package edu.mju.service.impl;

import edu.mju.dao.IUserDao;
import edu.mju.dao.impl.UserDaoImpl;
import edu.mju.entity.ScenicSpot;
import edu.mju.entity.User;
import edu.mju.service.IUserService;
import edu.mju.util.EncryptUtil;
import edu.mju.util.Pager;
import edu.mju.util.StrUtil;

/**
 * @Author:wyp
 * @Date:2020/5/8 23:52
 */
public class UserServiceImpl implements IUserService {
    IUserDao userDao = new UserDaoImpl();


    @Override
    public User findUser(User user) {
        user = userDao.findByUserName(user.getUserName());
        return user;
    }

    /**
     *  执行注册公司用户操作
     * @param user
     * @return 返回User到CompanyComtroller.companyAdd()中
     */
    @Override
    public User registerCompanyUser(User user) {
        user.setUsersalt(StrUtil.randString(32));
        user.setUserPassword(EncryptUtil.md5(user.getUserPassword()+user.getUsersalt()));
        return userDao.insertCompanyUser(user);
    }

    /**
     *
     * @param userName 用户名
     * @param userPassword 密码
     * @return user对象
     */
    @Override
    public User login(String userName, String userPassword) {
        User user = userDao.findByUserName(userName);
        if(user != null){
            String source = userPassword + user.getUsersalt();
            String encryStr = EncryptUtil.md5(source);
            if (encryStr.equals(user.getUserPassword())) {
                user.setUserPassword(null);
                user.setUsersalt(null);
                return user;
            }
        }
        return null;
    }

    @Override
    public boolean update(User user) {
        if(user.getUserPassword()!="" || !"".equals(user.getUserPassword())){
            user.setUsersalt(StrUtil.randString(32));
            user.setUserPassword(EncryptUtil.md5(user.getUserPassword()+user.getUsersalt()));
        }
        return userDao.updateFindUser(user);
    }

    @Override
    public boolean backupdate(User user) {
        if(user.getUserPassword()!="" || !"".equals(user.getUserPassword())){
            user.setUsersalt(StrUtil.randString(32));
            user.setUserPassword(EncryptUtil.md5(user.getUserPassword()+user.getUsersalt()));
        }
        return userDao.update2(user);
    }

    @Override
    public boolean save(User user) {
        if(user == null){
            System.out.println("user为空");
            return false;
        }
        user.setUsersalt(StrUtil.randString(32));
        user.setUserPassword(EncryptUtil.md5(user.getUserPassword()+user.getUsersalt()));
        if(user.getUserId() == null){
            return userDao.insert(user);
        }else {
            return userDao.update(user);
        }
    }

    @Override
    public boolean delete(Integer id) {
        return userDao.deleteById(id);
    }

    @Override
    public Pager pagernate(int page, int pageSize, User user) {
        Pager pager = userDao.pagenate(page,pageSize,user);
        return pager;
    }

    @Override
    public User findByUserId(Integer userId) {
        User user =userDao.findById(userId);
        return user;
    }
}
